#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##### Remember, this is a velocity template 
set -x
date +"%Y-%m-%d %T.%3N"
umask 0022
echo $$
ps -e -o pid,pgrp,user,args
ps x -o  "%p %r %y %x %c "
chmod -R u+w $logDir
rm -rf $logDir
# makes $logDir and $logDir/tmp
mkdir -p $logDir/tmp
if [[ -n "${javaHomeForTests}" ]]
then
  export JAVA_HOME=$javaHomeForTests
  export PATH=$JAVA_HOME/bin/:$PATH
elif [[ -n "${javaHome}" ]]
then
  export JAVA_HOME=$javaHome
  export PATH=$JAVA_HOME/bin/:$PATH
fi
export ANT_OPTS="-Xmx1g -XX:MaxPermSize=256m -Djava.io.tmpdir=$logDir/tmp ${antEnvOpts}"
export MAVEN_OPTS="-Xmx256m -Djava.io.tmpdir=$logDir/tmp ${mavenEnvOpts}"
export HADOOP_ROOT_LOGGER=INFO,console
export HADOOP_OPTS="-Dhive.log.dir=$logDir -Dhive.query.id=hadoop -Djava.io.tmpdir=$logDir/tmp"
cd $localDir/$instanceName/${repositoryName}-source || exit 1
date +"%Y-%m-%d %T.%3N"
echo "Pre test cleanup"
if [[ -s batch.pid ]]
then
  while read pid
  do
    if kill -9 -$pid 2>/dev/null
    then
      echo "Killed process group $pid"
      sleep 1
    fi
  done < batch.pid
fi
echo "$$" > batch.pid
find ./ -name 'TEST-*.xml' -delete
date +"%Y-%m-%d %T.%3N"
echo "Pre test cleanup done"
ret=0
if [[ "${buildTool}" == "maven" ]]
then
  if [[ -z "$testModule" ]]
  then
    testModule=./
  fi
  pushd $testModule
  timeout 40m mvn -B test -Dmaven.repo.local=$localDir/$instanceName/maven \
    $mavenArgs $mavenTestArgs $testArguments 1>$logDir/maven-test.txt 2>&1 </dev/null &
  date +"%Y-%m-%d %T.%3N"
#[[
  pid=$!
]]#
  popd
elif [[ "${buildTool}" == "ant" ]]
then
  timeout 40m ant ${antTestTarget} -Dtest.junit.output.format=xml \
    -Divy.default.ivy.user.dir=$localDir/$instanceName/ivy \
    -Divy.default.always.check.exact.revision=false \
    -Divy.cache.ttl.default=eternal -Divy.checkmodified=false \
    -Dmvn.local.repo=$localDir/$instanceName/maven \
    $antArgs $antTestArgs $testArguments 1>$logDir/ant-test.txt 2>&1 </dev/null &
#[[
  pid=$!
]]#
else
  echo "Unknown build tool ${buildTool}"
  exit 127
fi
echo $pid >> batch.pid
wait $pid
ret=$?
date +"%Y-%m-%d %T.%3N"
mkdir ${logDir}/logs
find ./ -type f -name 'TEST-*.xml' | \
xargs -I {} sh -c 'f=TEST-${batchName}-$(basename {}); test -f ${logDir}/$f && f=$f-$(uuidgen); mv {} ${logDir}/$f'
find ./ -type f -name hive.log -o -name spark.log -o -name derby.log | \
  xargs -I {} sh -c 'f=$(basename {}); test -f ${logDir}/logs/$f && f=$f-$(uuidgen); mv {} ${logDir}/logs/$f'
find ./ -path "*/spark/work" | \
  xargs -I {} sh -c 'mv {} ${logDir}/logs/spark-log'
find ./ -type f -name 'syslog*' | \
  xargs -I {} sh -c 'mkdir -p ${logDir}/logs/syslogs; mv {} ${logDir}/logs/syslogs'
date +"%Y-%m-%d %T.%3N"

if [[ -f $logDir/.log ]]
then
  mv $logDir/.log $logDir/logs/dot.log
fi
exit $ret
